package org.jeecg.modules.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.data.entity.TInvestigationRelease;
import org.jeecg.modules.data.service.ITInvestigationReleaseService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 示例不带参定时任务
 *
 * @Author Scott
 */
@Slf4j
public class SampleJob implements Job {

	@Resource
	private ITInvestigationReleaseService tInvestigationReleaseService;
	@Resource
	private ISysUserService sysUserService;
	@Resource
	private SysBaseApiImpl sysBaseApi;
	@Resource
	private ISysDictService sysDictService;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		QueryWrapper<TInvestigationRelease> queryWrapper = new QueryWrapper<>();
		queryWrapper.le("end_time", LocalDate.now());
		queryWrapper.eq("status", "1");
		List<TInvestigationRelease> list = tInvestigationReleaseService.list(queryWrapper);
		for (TInvestigationRelease tInvestigationRelease : list) {
			String text = sysDictService.queryDictTextByKey("crop_type", tInvestigationRelease.getCropType());
			String title = text + "【"+DateUtils.formatDate(tInvestigationRelease.getStartTime()) + "_" + DateUtils.formatDate(tInvestigationRelease.getEndTime()) +"】";
			// 将过期的数据进行更新状态
			tInvestigationRelease.setStatus("2");
			tInvestigationReleaseService.updateStatus(tInvestigationRelease);
			List<String> userList = sysUserService.getUserByRoleCode();
			for (String username : userList) {
				MessageDTO messageDTO = new MessageDTO();
				messageDTO.setFromUser("admin");
				messageDTO.setToUser(username);
				messageDTO.setTitle("待办事项");
				messageDTO.setContent("请注意，"+ title + "调查发布已过期！");
				sysBaseApi.sendSysAnnouncement(messageDTO);
			}
		}
	}
}
